package hr.ctg.domain.action;

import hr.ctg.domain.model.Jezik;
import hr.ctg.service.dao.JezikDAO;

import java.util.List;

import javax.faces.context.FacesContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.cache.CacheProvider;
import org.jboss.seam.international.LocaleSelector;

@Name("languageAction")
public class LanguageAction {
	@RequestParameter
	String l;

	@In
	LocaleSelector localeSelector;
	@In(required = false)
	FacesContext facesContext;
	@In
	CacheProvider cacheProvider;
	@In
	JezikDAO jezikDAO;

	@RequestParameter
	String locale;

	public void select() {
		if (l != null && !localeSelector.getLocaleString().equals(l)) {
			localeSelector.setCookieEnabled(true);
			localeSelector.setLocaleString(l);
			localeSelector.select();
		}
	}

	@Factory(value = "jezici", scope = ScopeType.APPLICATION, autoCreate = true)
	public List<Jezik> getJezici() {
		return jezikDAO.findAll();
	}

	@Factory(value = "currentLang", autoCreate = true)
	public String getCurrentLanguage() {
		// String lang;
		// if (facesContext == null) {
		// lang = Jezici.EN.toString();
		// } else {
		// lang = facesContext.getViewRoot().getLocale().getLanguage();
		// }
		return localeSelector.getLocaleString();
		// if (lang == null) {
		// lang = Jezici.EN.toString();
		// }
		// return lang;
	}
}
